package com.eorchis.module.classdept.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.module.classdept.dao.IClassDeptDao;
import com.eorchis.module.classdept.domain.ClassDept;
import com.eorchis.module.classdept.domain.ClassDeptQueryBean;
import com.eorchis.module.classdept.service.IClassDeptService;
import com.eorchis.module.classdept.ui.commond.ClassDeptQueryCommond;
import com.eorchis.module.classdept.ui.commond.ClassDeptValidCommond;
import com.eorchis.module.classuser.dao.IClassUserDao;
import com.eorchis.module.classuser.ui.commond.ClassUserQueryCommond;
import com.eorchis.module.deptassignedinfo.dao.IDeptAssignedInfoDao;
import com.eorchis.module.trainingclass.service.ITrainingClassService;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassValidCommond;

/**
 * Title: ClassDeptServiceImpl<br>
 * Description: 班级部门关系表管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-12-30
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.classdept.service.impl.ClassDeptServiceImpl")
public class ClassDeptServiceImpl 
			extends AbstractBaseService 
		implements IClassDeptService {
		
	@Autowired
	@Qualifier("com.eorchis.module.classdept.dao.impl.ClassDeptDaoImpl")
	private IClassDeptDao classDeptDao;
	
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.service.impl.TrainingClassServiceImpl")
	private ITrainingClassService trainingClassService;
	
	@Resource(name = "com.eorchis.module.deptassignedinfo.dao.impl.DeptAssignedInfoDaoImpl")
	private IDeptAssignedInfoDao deptAssignedInfoDao;
	
	@Resource(name = "com.eorchis.module.classuser.dao.impl.ClassUserDaoImpl")
	private IClassUserDao classUserDao;

	public IDaoSupport getDaoSupport() {
		return classDeptDao;
	}
	@SuppressWarnings("unchecked")
	public ClassDeptValidCommond toCommond(IBaseEntity entity) {
		return new ClassDeptValidCommond((ClassDept)entity);
	}
	
	/**
	 * 查询未选部门列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-30
	 */
	@Override
	public List<ClassDeptQueryBean> findNoSeleDeptList(
			ClassDeptQueryCommond qCommond) throws Exception {
		return classDeptDao.findNoSeleDeptList(qCommond);
	}
	
	/**
	 * 查询班级已选部门列表
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-30
	 */
	@Override
	public List<ClassDeptQueryBean> findSeleDeptList(
			ClassDeptQueryCommond qCommond) throws Exception {
		return classDeptDao.findSeleDeptList(qCommond);
	}
	
	
	/**
	 * 班级添加报名单位
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@Override
	public boolean addDeptForClass(
			final ClassDeptValidCommond vCommond) throws Exception {
		return classDeptDao.addDeptForClass(vCommond);
	}
	
	/**
	 * 删除班级报名单位关系
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@Override
	public String deleteClassDept(ClassDeptValidCommond vCommond)
			throws Exception {
		String cdi=vCommond.getSearchClassDepIds()[0];
		ClassDept cd=classDeptDao.find(ClassDept.class, cdi);
		String classId=cd.getTrainingClass().getClassId();
		ClassUserQueryCommond cuqc=new ClassUserQueryCommond();
		//判断单位时候有人员报名
		for(int i=0;i<vCommond.getSearchClassDepIds().length;i++) {
			String depid=vCommond.getAddDepIds()[i];
			cuqc.setSearchClassID(classId);
			cuqc.setSearchDepId(depid);
			Long num=classUserDao.getCountSelectedUserList(cuqc);
			if(num>0) {
				return "所选单位中已有学员报名，不能删除！";
			}
		}
				
		classDeptDao.deleteClassDept(vCommond);
		return "";
	}
	
	/**
	 * 删除班级部门关系
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2014-12-31
	 */
	@Override
	public void delDeptByClassId(ClassDeptValidCommond vCommond)
			throws Exception {
		classDeptDao.delDeptByClassId(vCommond);
	}
	
	
	/**
	 * 修改班级人数
	 * @param vCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2015-1-7
	 */
	@Override
	public String updateDeptUserNum(ClassDeptValidCommond vCommond)
			throws Exception {
		String searchDepId=vCommond.getSearchDepId();//修改部门
		String searchClassType=vCommond.getSearchClassType();//班级类型
		
		if(searchClassType.equals("gfb")) {
			searchClassType="2";
		}else {
			searchClassType="1";
		}
		//查询今年这个班级类型下这个部门的参学名额
		TrainingClassValidCommond co=trainingClassService.find(vCommond.getSearchClassId());
		String searchYear=co.getClassnameYear();//年份
		Integer num=deptAssignedInfoDao.getNumByYearAndDept(searchDepId, searchClassType, searchYear);
		if(num.equals(0)) {
			return "单位没有设置限额";
		}
		//如果单位有设置限额，查出这个单位在定向班中已经分配出多少限额
//		Integer useNum=classDeptDao.findSeleNumListByDepId(vCommond.getSearchClassId(), searchDepId, searchYear,searchClassType);
//		Integer syNum=num-useNum;
//		if(syNum<0) {
//			syNum=0;
//		}
//		if(vCommond.getLimitNum()>syNum) {
//			return "超出单位设置限额,可用名额为:"+syNum+"人";
//		}  暂时注释掉这段逻辑 by Max at 2015-02-11
		
		if(vCommond.getLimitNum()>num) {
			return "超出单位设置限额,本单位限额为:"+num+"人";
		}
		
		//班级人数判断
		Integer classNum=co.getClassUserNum();//班级总人数
		//查询去除这个单位后指定单位的总人数 
		ClassDeptQueryCommond qCommond=new ClassDeptQueryCommond();
		qCommond.setSearchClassId(vCommond.getSearchClassId());
		qCommond.setSearchNoDepId(searchDepId);	
		Integer allNum=classDeptDao.findSeleDeptNumList(qCommond);
		if(classNum<allNum+vCommond.getLimitNum()) {
			return "超出班级总人数限额";
		}
		deptAssignedInfoDao.updateClassDeptNum(vCommond);
		return "";
	}
	
	/**
	 * 查询班级已选部门列表中报名已分配的总人数
	 * @param commond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @createDate 2015-1-23
	 */
	@Override
	public Integer findSeleDeptNumList(
			ClassDeptQueryCommond qCommond) throws Exception {
		return classDeptDao.findSeleDeptNumList(qCommond);
	}
	
}
